<template>
 <el-container class="home-Container">
  <el-header>
    <div>
      <img src="../assets/heima.png" alt="">
      <span>电商后台管理系统</span>
    </div>
  </el-header>
  <el-container>
    <el-aside :width="isTrue ? '64px' : '200px'">
      <!-- 实现菜单的折叠和展开的按钮 -->
      <div class="toggle-button" @click="toggleCollapse">III</div>
       <!-- unique-opened表示每次只能展开一个子菜单 -->
       <el-menu
      background-color="#333744"
      text-color="#fff"
      active-text-color="#409EFF"
      unique-opened
      :collapse="isTrue"
      :collapse-transition="false"
      :router="true"
      :default-active="activePath">
      <!-- 这是一级菜单区域 -->
      <el-submenu :index="item.id+''" v-for="item in menulist" :key="item.id">
        <!-- 这是一级菜单的模板区域 -->
        <template slot="title">
          <!-- 这是一级菜单的图标 -->
          <i :class="iconsObj[item.id]"></i>
          <!-- 这是一级菜单的字体 -->
          <span>{{item.authName}}</span>
        </template>
        <!-- 这是二级菜单下面的二级菜单模板区域 -->
        <!-- 双层循环嵌套，拿到二级菜单 -->
        <el-menu-item :index="'/' + subItem.path" v-for="subItem in item.children" :key="subItem.id" @click="SaveNavState('/' + subItem.path)">
        <!-- 这是二级菜单的模板区域 -->
        <template slot="title">
          <!-- 这是二级菜单的图标 -->
          <i class="el-icon-menu"></i>
          <!-- 这是二级菜单的字体 -->
          <span>{{subItem.authName}}</span>
        </template></el-menu-item>
      </el-submenu>
    </el-menu>
    </el-aside>
    <el-container>
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</el-container>
</template>

<script>
export default {
  data() {
    return {
      // 左侧菜单数据
      menulist: [],
      iconsObj: {
        125: 'iconfont icon-user',
        103: 'iconfont icon-tijikongjian',
        101: 'iconfont icon-shangpin',
        102: 'iconfont icon-danju',
        145: 'iconfont icon-baobiao'
      },
      isTrue: false,
      activePath: ''
    }
  },
  // 采用生命周期函数,表示在页面加载完成加载出左侧菜单
  created() {
    this.getMenuList()
    // 在组件页面一开始加载时就取出那个存储的activePath
    this.activePath = window.sessionStorage.getItem('activePath')
  },
  methods: {
    logout() {
      // 清空token里面的值
      window.sessionStorage.clear()
      // 用编程式导航进行跳转
      this.$router.push('/login')
    },
    // 获取左侧菜单的函数
    async getMenuList() {
      // 将data解构出来并且重命名为res
      const { data: res } = await this.$http.get('menus')
      // const res = await this.$http.get('menus')
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      // 数组赋值
      this.menulist = res.data
      console.log(res.data)
      // console.log(res.data)
      // console.log(res.meta)
    },
    toggleCollapse() {
      this.isTrue = !this.isTrue
    },
    SaveNavState(activePath) {
      // 先将需要用到的二级标题路径保存到sessionStorage当中
      window.sessionStorage.setItem('activePath', activePath)
      this.activePath = activePath
    }
  }
}
</script>

<style lang="less" scoped>
.home-Container {
  height: 100%;
}
.el-header {
  background-color: #373d41;
  display: flex;
  justify-content: space-between;
  padding-left: 0;
  align-items: center;
  color: #fff;
  font-size: 20px;
  // 嵌套样式
  > div {
    display: flex;
    align-items: center;
    span {
      margin-left: 15px;
    }
  }
}
.el-header {
 background-color: #373d41;
}
.el-aside {
  background-color: #333744;
  .el-menu {
    border-right: none;
  }
}
.el-main {
  background-color: #eaedf1;
}
.iconfont {
  margin-right: 10px;
}
.toggle-button {
  background-color: #4a5064;
  font-size: 10px;
  line-height: 24px;
  color: #fff;
  text-align: center;
  letter-spacing: 0.2em;
  cursor: pointer;
}
</style>
